package com.jd.cmdb.tsc.schedule;

import com.jd.cmdb.tsc.service.ProcessorService;
import com.jd.cmdb.tsc.zk.service.ZKService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by tianzhengguo on 2016/1/20.
 */
@Service
public class DefaultScheduleServiceImpl implements ScheduleService {
    private static final Logger logger = LoggerFactory.getLogger(DefaultScheduleServiceImpl.class);


    @Resource
    private ProcessorService processorService;

    @Resource
    private ZKService zkService;

    @Override
//    @Scheduled(cron = "${task.cron}")
    public void run() throws Exception {
        try{
            if(!zkService.isLeader()){
                logger.info("当前不是leader");
                return ;
            }
            processorService.execute();
        }catch(Exception e){
            logger.error("执行过程出错:{},原因:{}",e.getMessage(),e.toString());
        }
    }
}
